﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;
using Yqd.Enm;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxJobNodeController : Controller
    {
        YqdContext ctx;

        public WxJobNodeController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetList()
        {
            var AssignID = Form.GetInt("AssignID");
            var data = ctx.Database.SqlQuery<Yqd_JobNode>("select * from Yqd_JobNode where [AssignID]={0} order by [Instime] asc", AssignID);
            return this.ToJson(new
            {
                success = true,
                totalcount = data.Count(),
                data = data.ToList().Select(it => new
                {
                    it.NodeID,
                    it.Require,
                    PlanCompleteTime = it.PlanCompleteTime?.ToString("yyyy.MM.dd") ?? "",
                    Status = it.Status.ToString(),
                    it.CompleteCount,
                    it.CompletingCount
                })
            });
        }

        /// <summary>
        /// 添加内部流转
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Add(Yqd_JobNode model)
        {
            var SuperviseID = Form.GetInt("SuperviseID");
            var CoordinateID = Form.GetInt("CoordinateID");
            var eSuperviseID = Form.GetInt("eSuperviseID");
            var sSuperviseID = Form.GetInt("sSuperviseID");
            model.ItemID = 0;
            model.ItemType = 0;
            var IsJb = false;
            if (SuperviseID > 0)
            {
                model.ItemID = SuperviseID;
                model.ItemType = 事项类型.钉办督导;
            }
            if (CoordinateID > 0)
            {
                model.ItemID = CoordinateID;
                model.ItemType = 事项类型.申请协调;
            }
            if (eSuperviseID > 0)
            {
                model.ItemID = eSuperviseID;
                model.ItemType = 事项类型.精准督查;
            }
            if (sSuperviseID > 0)
            {
                IsJb = true;
                model.ItemID = sSuperviseID;
                model.ItemType = 事项类型.专项交办;
            }
            model.Status = 工作节点完成情况.未完成;
            model.Instime = DateTime.Now;
            model.EmployeeCode = WxUser.LogonInfo.EmployeeCode;
            model.UnitCode = WxUser.LogonInfo.UnitCode;
            ctx.Yqd_JobNode.Add(model);
            var ret = ctx.SaveChanges();
            if (ret > 0)
            {
                TaskLogService.AddLog(IsJb ? sSuperviseID : model.TaskID, "设置了(" + model.Require + ")工作节点计划。", IsJb);
            }
            return this.ToJson(new
            {
                success = true,
                model.NodeID,
                ret
            });
        }

        public ActionResult Get()
        {
            var NodeID = Form.GetInt("NodeID");
            var m = ctx.Yqd_JobNode.FirstOrDefault(it => it.NodeID == NodeID);
            if (m != null)
            {
                return this.ToJson(new
                {
                    success = true,
                    data = new
                    {
                        m.NodeID,
                        m.Require,
                        PlanCompleteTime = m.PlanCompleteTime.ToString("yyyy-MM-dd"),
                        m.CompleteCount,
                        m.CompletingCount
                    }
                });
            }
            return this.ToJson(new
            {
                success = false,
                msg = "工作节点不存在"
            });
        }

        /// <summary>
        /// 修改工作节点
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Edit(Yqd_JobNode model)
        {
            var s = ctx.Yqd_JobNode.FirstOrDefault(it => it.NodeID == model.NodeID);
            if (s != null)
            {
                s.Require = model.Require;
                s.PlanCompleteTime = model.PlanCompleteTime;
                var IsAll = Form.GetBool("IsAll", true);
                if (IsAll)
                {
                    s.CompleteCount = model.CompleteCount;
                    s.CompletingCount = model.CompletingCount;
                }
            }
            var ret = ctx.SaveChanges();
            if (ret > 0)
            {
                var IsJb = s.ItemType == 事项类型.专项交办;
                TaskLogService.AddLog(IsJb ? model.ItemID : model.TaskID, "修改了(" + model.Require + ")工作节点计划。", IsJb);
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        /// <summary>
        /// 更新工作节点完成情况
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult UpdateStatus()
        {
            var NodeID = Form.GetInt("NodeID");
            var status = Form.GetInt("Status");
            var ret = ctx.Database.ExecuteSql("update Yqd_JobNode set Status={1},CompleteTime=getdate() where NodeID={0};", NodeID, status);
            if (ret > 0)
            {
                var n = ctx.Yqd_JobNode.FirstOrDefault(it => it.NodeID == NodeID);
                if (n != null)
                {
                    var IsJb = n.ItemType == 事项类型.专项交办;
                    TaskLogService.AddLog(IsJb ? n.ItemID : n.TaskID, "完成了(" + n.Require + ")工作节点计划。", IsJb);
                }
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        /// <summary>
        /// 更新工作节点完成情况
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult UpdateComplete()
        {
            var NodeID = Form.GetInt("NodeID");
            var CompleteCount = Form.GetInt("CompleteCount");
            var CompletingCount = Form.GetInt("CompletingCount");
            var ret = ctx.Database.ExecuteSql("update Yqd_JobNode set CompleteCount={1},CompletingCount={2} where NodeID={0};", NodeID, CompleteCount, CompletingCount);
            if (ret > 0)
            {
                var n = ctx.Yqd_JobNode.FirstOrDefault(it => it.NodeID == NodeID);
                if (n != null)
                {
                    var IsJb = n.ItemType == 事项类型.专项交办;
                    TaskLogService.AddLog(IsJb ? n.ItemID : n.TaskID, "填报了(" + n.Require + ")工作节点计划的完成数和当前完成数。", IsJb);
                }
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        [User]
        public ActionResult Delete()
        {
            var NodeID = Form.GetInt("NodeID");
            var n = ctx.Yqd_JobNode.FirstOrDefault(it => it.NodeID == NodeID);
            var ret = ctx.Database.ExecuteSql("delete from Yqd_JobNode where NodeID={0};", NodeID);
            if (ret>0 && n != null)
            {
                var IsJb = n.ItemType == 事项类型.专项交办;
                TaskLogService.AddLog(IsJb ? n.ItemID : n.TaskID, "删除了(" + n.Require + ")工作节点计划。", IsJb);
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
    }
}
